package org.eking.bigdata.hive;

import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import java.sql.DriverManager;
import org.apache.hive.jdbc.*;
public class HiveTest {

	
	public  HiveTest(){
		
	}
	
	public  HiveTest(String Url, String UserName, String Passwd){
		this.Url = Url;
		this.UserName = UserName;
		this.Passwd = Passwd;
		
	}
	
	public  HiveTest(String Url){
		this.Url = Url;
	}
	
	private String Url;
	private String UserName = "";
	private String Passwd = ""; 
	 private static String driverName = "org.apache.hive.jdbc.HiveDriver";
	private Connection con;
	private Statement stmt;
	/** 日志 */
    static final Logger logger = LoggerFactory.getLogger(HiveTest.class);
    
	public Boolean Conn() throws SQLException{
		if(Url == null || Url == ""){
			logger.error("Connect Url is null");
			return false;
		}
		
		try {
            Class.forName(driverName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.exit(1);
        }
		
		String JdbcUrl = String.format("jdbc:hive2://%s", Url);
		
		con = DriverManager.getConnection(JdbcUrl, UserName, Passwd);
		stmt = con.createStatement();		
		return true;
	}	
	
	
	public ResultSet RunCtx(String SqlStr) throws SQLException{	
		ResultSet res;
		res = stmt.executeQuery(SqlStr);
		return res;
	}
	
	public Boolean DisCon() {
		try {
			if(stmt.isClosed()){
				stmt.close();
			}
			if(con.isClosed()){
				con.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return true;
	}
}


